package com.homelink.android.explorerup.adapter;

import android.util.Log;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.homelink.android.explorerup.component.FileDetails;
import com.homelink.android.explorerup.view.NumOfImageView;

public abstract class BaseFileListAdapter extends BaseAdapter {
	public static final String TAG = "BaseFileListAdapter";

	private FileDetails[] mFiles;
	private String mBasePath;
	private View.OnClickListener mOnClickListener;
	private View.OnTouchListener mOnTouchListener;
	private View.OnLongClickListener mOnLongClickListener;
	private boolean mIsSelectEnable = false;
	private boolean mIsSelectedAll = false;

	public static enum Types {
		Simple, Details, Image, DetailImage
	};
	
	public void setOnLongClickListener(View.OnLongClickListener l){
		mOnLongClickListener = l;
	}
	
	public View.OnLongClickListener getItemOnLongClickListener(){
		return mOnLongClickListener;
	}

	public void setOnTouchListener(View.OnTouchListener listener){
		this.mOnTouchListener = listener;
	}
	
	public boolean getSelected(int position) {
		if (mFiles != null && mFiles.length > position) {
			return mFiles[position].selected;
		}
		return false;
	}

	public boolean isSelectedAll() {
		return mIsSelectedAll;
	}

	public void select(int position, boolean selected) {
		if (mFiles != null && mFiles.length > position) {
			mFiles[position].selected = selected;
		}
		Log.d(TAG, "select : " + position + " selected: " + selected);
		mIsSelectedAll = false;
	}

	public void selectAll(boolean selected) {
		if (mFiles != null) {
			for (int i = 0; i < mFiles.length; i++) {
				mFiles[i].selected = selected;
			}
		}
		mIsSelectedAll = selected;
	}

	public void setSelectEnable(boolean enable) {
		mIsSelectEnable = enable;
	}

	public boolean isSelectEnable() {
		return mIsSelectEnable;
	}

	public void setBasePath(String basePath) {
		mBasePath = basePath;
	}

	public String getBasePath() {
		return mBasePath;
	}

	/**
	 * 
	 * @return
	 */
	public abstract Types getType();

	public void setFiles(FileDetails[] files) {
		mFiles = files;
		notifyDataSetChanged();
	}

	public int getCount() {
		if (mFiles != null) {
			return mFiles.length;
		}
		return 0;
	}

	public Object getItem(int position) {

		return mFiles[position];
	}

	public long getItemId(int position) {
		return position;
	}

	public void setOnItemClickListener(View.OnClickListener listener) {
		this.mOnClickListener = listener;
	}

	public View.OnClickListener getItemClickListener() {
		return mOnClickListener;
	}
	
	public View.OnTouchListener getItemTouchListener(){
		return  mOnTouchListener;
	}

	public static class ViewHolder {
		public NumOfImageView iconImage;
		public TextView nameView;
		public String nameTxt;
		public boolean isFolder = false;
		public int itemIndex = -1;

		public void reset() {
			itemIndex = -1;
			if (iconImage != null) {
				iconImage.reset();
			}
			if (nameView != null) {
				nameView.setText("");
			}
			nameTxt = null;
			isFolder = false;

		}
	}

}
